﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DoubanApiLib.OAuth;
using DoubanApiLib.Access.Account;
using System.Xml;
using System.Data;
using DoubanApiLib.Entity;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using DoubanApiLib.Base;
namespace ApiTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string acc = "";
            string accsec = "";
            OAuthModule.Test(out acc, out accsec);
            //---获取用户朋友的时候会抛500 错~
            DataSet dss = AccountAccess.GetUserFriends("lifeifei", 1, 10, acc, accsec);


            //List<Account> lstacc = Account.CreateInstanceList(ds);
            //Account acc = Account.CreateInstance(ds);
            #region 模拟数据包登陆 获取豆瓣电台数据

            //CookieContainer cc = new CookieContainer();
            //QzoneOperation.HttpHelper.GetHtml("http://www.douban.com/", cc);
            //string html = QzoneOperation.HttpHelper.GetHtml("https://www.douban.com/accounts/login", "source=simple&form_email=hanner%40live.cn&form_password=noweikili9&user_login=%E7%99%BB%E5%BD%95", true, cc);
            //string u = QzoneOperation.HttpHelper.GetHtml("http://douban.fm/j/mine/playlist?type=s&sid=1023370&h=|451941:r|552456:s|659690:s|1451882:r|425695:s|1023370:s&channel=1&r=84ef0dff11", cc);
            //JObject obj = JObject.Parse(u);
            //JArray a = (JArray)obj["song"];

            //List<string> urls = new List<string>();
            //foreach (JToken item in a)
            //{
            //    foreach (JContainer item_sub in item)
            //    {
            //        string name = item_sub.GetPropertyValue<string>("Name");

            //        string value = item_sub.GetPropertyValue<string>("Value");
            //        if (name == "picture")
            //        {
            //            Program.ConvertStr(value, ref value);
            //            int le = 0;
            //            Stream s = QzoneOperation.HttpHelper.GetStream(value, out le);
            //            BinaryReader br = new BinaryReader(s);
            //            byte[] data = br.ReadBytes(le);
            //            string filename = "d:\\download\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(value);
            //            FileStream fs = new FileStream(filename, FileMode.Create);
            //            fs.Write(data, 0, data.Length);
            //            fs.Close();
            //        }
            //        if (name == "url")
            //        {
            //            Program.ConvertStr(value, ref value);
            //            int le = 0;
            //            Stream s = QzoneOperation.HttpHelper.GetStream(value, out le);
            //            BinaryReader br = new BinaryReader(s);
            //            byte[] data = br.ReadBytes(le);
            //            string filename = "d:\\download\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(value);
            //            FileStream fs = new FileStream(filename, FileMode.Create);
            //            fs.Write(data, 0, data.Length);
            //            fs.Close();
            //            urls.Add(value);
            //        }
            //    }

            //}
            #endregion

            //DataSet ds = AccountAccess.SelectUsersInfo("今夜", 0, 20);
            //List<Account> accSelectResult = Account.CreateInstanceList(ds);
            //PageInfo page = PageInfo.CreateInstance(ds);
            //PageContainer<Account> pagec = new PageContainer<Account>();
            //pagec.Setup<Account>(ds);

            //--模糊查找 用户名 含 今夜的人 取第一页数据1，每页20条 到dataset集合
            DataSet ds = AccountAccess.SelectUsersInfo("今夜", 1, 20);

            //这里省略了大家对dataset数据的处理N行
            
            //-----------------分页数据可以这样做-----------------------------
            PageContainer<Account> pagec = new PageContainer<Account>();
            //PageContainer装载获取的data数据
            pagec.Setup<Account>(ds);
            //--用于绑定的数据
            List<Account> lstData = pagec.DataSource;
            //--下面封装了分页信息
            //pagec.PageSize;
            //pagec.PageIndex;
            //pagec.RecordCount;
            //pagec.PageSize;
            
            Console.ReadKey();
        }


        public static bool ConvertStr(string likeStr, ref string outStr)
        {
            bool isStr = false;
            if (likeStr.StartsWith("\"") && likeStr.EndsWith("\""))
            {
                isStr = true;
                outStr = outStr.Substring(1, outStr.Length - 1);
                outStr = outStr.Substring(0, outStr.Length - 1);

            }
            return isStr;
        }
    }
}
